package com.xiaohei.auth.util;

import com.xiaohei.model.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: MenuHelper
 * Package:
 * Description:
 *
 * @Author:xiaohei
 * @Create 2024/4/25 21:28
 * Version 1.0
 */
public class MenuHelper {

    public static List<SysMenu> buildTreeList(List<SysMenu> sysMenuList) {
        //递归方法构建菜单
        List<SysMenu> listTrees = new ArrayList<SysMenu>();
        //遍历
        for (SysMenu sysMenu : sysMenuList) {
            //递归操作
            if(sysMenu.getParentId().longValue() == 0){
                listTrees.add(getChildren(sysMenu,sysMenuList));
            }
        }
        return listTrees;
    }

    private static SysMenu getChildren(SysMenu sysMenu, List<SysMenu> sysMenuList){

        sysMenu.setChildren(new ArrayList<>());
        //遍历所有菜单数据判断id和parentID的对应关系
        for (SysMenu it : sysMenuList) {
            if(it.getParentId().longValue() == sysMenu.getId().longValue()){
                if(sysMenu.getChildren() == null){
                    sysMenu.setChildren(new ArrayList<>());
                }
                sysMenu.getChildren().add(getChildren(it,sysMenuList));
            }
        }
        return sysMenu;
    }
}
